package cn.zr.aitool.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.zr.aitool.utils.Utils;
import cn.zr.aitool.bean.NavText;

/**
 * Description: GroupCursorManager.java Create on 2013-5-14 上午10:27:25
 *
 * @author wk
 * @version 1.0 Copyright (c) 2013 Company,Inc. All Rights Reserved.
 */
public class NavTextManager extends DbBaseManager{

    private static NavTextManager mManager;

    private NavTextManager() {
        super(DataBaseHelper.TOOL_NAV_TEXT);
    }

    public static NavTextManager getInstance() {
        if (mManager == null) {
            mManager = new NavTextManager();
        }
        return mManager;
    }

    public void copyTextToDb(Context contxt){
        String dirPath = Environment.getExternalStorageDirectory().toString() + File.separator + "aAitool";
        File dir = new File(dirPath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        String filePath = dirPath + File.separator +"lizhi.txt";
        File file = new File(filePath);
        if(!file.exists()){
            return;
        }
        ArrayList<String> texts = Utils.readTxtFile(filePath);
        List<NavText> groups = new ArrayList<>();
        NavText text;
        for (String content:texts) {
            text = new NavText();
            text.setText(content);
            groups.add(text);
        }
        insertGroup(contxt, groups);
        file.delete();
    }

    /**
     * 新建分组
     */
    public long insertGroup(Context context, ContentValues contentValues) {
        return insertSigle(context, contentValues);
    }

    public NavText queryTextById(Context context) {
        Random rand = new Random();
        int i = rand.nextInt(49) + 1;
        List<NavText> texts = queryGroupsBy1Selection(context,NavTexColumns.ID,String.valueOf(i));
        return texts.size()>0?texts.get(0):new NavText("小工具");
    }


    public List<NavText> queryGroupsBy1Selection(Context context, String selectionRow, String values) {
        return queryGroupsByTwoSelection(context, new String[]{selectionRow}, new String[]{values});
    }


    public List<NavText> queryGroupsByTwoSelection(Context context, String[] selectionRow, String[] selectionArgs) {
        Cursor cursor =queryByTwoSelecionByAnd(context, selectionRow, selectionArgs, null,null);
        return cursor2Group(cursor);
    }

    public void deleteGroupByOrgId(Context context, int orgId) {
        String selection = NavTexColumns.ID + "=?";
        String[] selectionArgs = new String[]{String.valueOf(orgId)};
        delete(context, selection, selectionArgs);
    }

    public void insertGroup(Context context, List<NavText> groups) {
        List<ContentValues> values = new ArrayList<ContentValues>();

        for(NavText group:groups){
            values.add(group.toContentValues());
        }
        insertAll(context, values);
    }

    public List<NavText> cursor2Group(Cursor cursor) {
        List<NavText> groups = new ArrayList<NavText>();
        if (cursor == null) {
            return groups;
        }

        int idIndex = cursor.getColumnIndex(NavTexColumns.ID);
        int textIndex = cursor.getColumnIndex(NavTexColumns.TEXT);
        NavText group;
        while (cursor.moveToNext()) {
            group = new NavText();
            group.setId(cursor.getInt(idIndex));
            group.setText(cursor.getString(textIndex));
            groups.add(group);
        }
        closeCursor(cursor);
        return groups;
    }

}
